package app.netant.book.xs37;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.enviroment.Enviroment;
import cn.lily.com.lang.number.MathUtil;
import app.netant.NetAntBase;

public class Xs37Page extends NetAntBase {

	public Xs37Page(String netSite) {
		super(netSite);
	}
	
	/**
	 * 信息解析
	 */
	public void analyse(){
		
		Elements activePart = document.select("#conn #main .list_center #sitebox dl");
		if(activePart.size()>0){
			for(int i=0;i<activePart.size();i++){	
				
				Enviroment.getEnv().getLogBean().beginLog();
				
				Element dlPart = activePart.get(i);
				
				Xs37Entity entity = new Xs37Entity();
				
				String href = dlPart.select("dt a").get(0).attr("href");
				String date = dlPart.select("dd h3 span.uptime").text();
				String tags = dlPart.select("dd.book_other").text();
				
				this.analyseTags(tags,entity);
				
				System.out.println("【Date】:"+date);
				
				entity.setNovelDate(date);
				
				System.out.println(href + "地址开始处理.........");
				
				Xs37Detail detail = null;
				try {
					
					DataTable dataTable = this.getDBManager().executeQueryTable("select * from ${schema}.NET_XS37_INFO where NOVEL_URL='"+href+"'");
					if(dataTable.next()){
						continue;
					}
		
					entity.setNovelUrl(href);
					
					detail = new Xs37Detail(href);		
					detail.setEntity(entity);
					
					detail.analyse();
					
				} catch (Exception e) {
					
					e.printStackTrace();
					
					DBFactory.getThreadDB().executeUpdate("insert into ${schema}.NET_XS37_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
														+ "values ('"+System.currentTimeMillis()+""+MathUtil.random(3)+"','"+href+"','2')");
					
					continue;
					
				} finally {
					if(detail != null){
						detail.free();
					}
				}
				
				Enviroment.getEnv().getLogBean().endLog();

			}
		}
			
	}
	
	public void analyseTags(String tagAlls,Xs37Entity entity){
		
		String[] tagm = tagAlls.split("：");
		String keys = "";
		String vals = "";
		
		for(int i=0;i<tagm.length;i++){
			
			String tmp = tagm[i].trim();
			
			if(i==0){
				keys += "," + tmp.replaceAll(" ", "");
			}
			else if(i==tagm.length-1){
				vals += "," + tmp.replaceAll(" ", "");
			}
			else{
				
				int first = tmp.indexOf(" ");
				if(first>0){
					keys += "," + tmp.substring(first).replaceAll(" ", "");
					vals += "," + tmp.substring(0,first).replaceAll(" ", "");
				}
				
			}
			
		}
		
		if(!"".equals(keys)){
			keys = keys.substring(1);
			vals = vals.substring(1);
		}
		
		String[] keyS = keys.split(",");
		String[] valS = vals.split(",");
		
		for(int j=0;j<keyS.length;j++){
			
			System.out.println("【Tag】"+keyS[j]+"="+valS[j]);
			
			entity.setTag(keyS[j], valS[j]);
			
		}	
		
	}
	
	public static void main(String[] args) {
		
		Enviroment.createEnv();
		Enviroment.getEnv().getLogBean().beginLog();
		
		Xs37Page page = new Xs37Page("https://www.37xs.com/list/1/2.html");
		page.analyse();
		
		Enviroment.getEnv().getLogBean().endLog();
		
		
	}
}
